package com.hongchen.wechat.controller;

import com.hongchen.model.ActGame;
import com.hongchen.model.ActGamePrize;
import com.hongchen.model.ActPrize;
import com.hongchen.model.ActUser;
import com.hongchen.wechat.config.Api;
import com.hongchen.wechat.service.RichService;
import com.jfinal.weixin.sdk.api.ApiResult;


import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by 录康哲 on 2018/1/24.
 */
public class TreasureController extends Api {
    private final RichService richService=enhance(RichService.class);
    ActUser actU = new ActUser();//存储用户信息便于使用

    ApiResult apiResult;


    public void index(){
        this.redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId()+"&redirect_uri=http://"+chengDong()+".tunnel.echomod.cn/treasure/txt&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect");
    }

    public void txt(){
        apiResult=userdetail();
        //开关
        boolean ok=false;
        if(apiResult.getStr("openid")!=null){
            String nickName=apiResult.get("nickname");
            String headImg=apiResult.get("headimgurl");
            String tel=apiResult.get("tel");
            int sex=apiResult.getInt("sex");
            String city=apiResult.get("city");
            String openId=apiResult.get("openid");

            if(openId != null){
                actU.setWechatOpenid(openId);
            }
            if(headImg != null){
                actU.setHeadImg(headImg);
            }
            if(tel != null){
                actU.setTel(tel);
            }
            if(nickName != null){
                actU.setWechatNickname(nickName);
            }
            actU.setSex(sex);

            if(city != null){
                actU.setCity(city);
            }

            //判断用户是否已经在数据库存在
            ActUser user = richService.findByOpenid(openId);

            if(user == null){//新增
                actU.setCreateTime(new Date());
                if(actU.save()){
                    ActGame actGame=new ActGame();//表示用户准备玩游戏
                    actGame.setActUserId(actU.getId());
                    actGame.setNowTime(new Date());
                    actGame.save();
                }
            }else{//老用户
                actU.setId(user.getId());
                user = actU;
                user.setUpdateTime(new Date());
                user.update();//更新用户信息成功
            }
            setAttr("openid",openId);
        }else{
            ok=true;
        }

        //进行用户信息判断
        if(ok){
            index();
        }else{
            render("index.html");
        }
    }

    /**
     * 随机文章跳转
     */
    public void href(){
       String href="";
        int rand =  new Random().nextInt(100);//开始游戏
        if(rand<=20){
           href ="http://www.baidu.com";
        }else if(rand<=40){
            href="http://g.cn";
        }else if(rand<=60){
            href="https://www.google.com.hk/";
        }else if(rand<=80){
            href="http://www.bilibili.com";
        }
        System.out.println(rand);
       setAttr("success",true);
       setAttr("href",href);
       renderJson();
    }

    public void ajax(){
        Long prize = 0L;//定义用户中的奖品
        String name=getPara("keyword");//用户输入的内容
        int bz=1;//未中奖
        if(name.equals("bz")){
            String openid=getPara("id");//根据用户openid获取信息
            ActGame actg = richService.finds(richService.findByOpenid(openid).getId());//查询用户关联游戏表
            int rand =  new Random().nextInt(100);//开始游戏
            if( rand >= 20){//中奖
                List<ActPrize> apList = richService.typePrize(4);//查询文字寻宝奖品集合
                if(apList != null){
                    ActPrize ap = richService.isPrize(apList);//奖品
                    if(ap != null){
                        ActGamePrize agp = new ActGamePrize();
                        agp.setGameId(actg.getId());
                        agp.setPrizeId(ap.getId());
                        agp.setPrizeNum(richService.lastPrizeNo()); //中奖编号
                        agp.setAwardTime(new Date());//当前中奖时间
                        agp.setAcceptTime(BigWheelController.getFetureDate(7));//生成7天后的领奖截止时间
                        agp.setAcceptAddres("河南省郑州市英协路56号传媒创意中心14层河南新维思广告有限公司");
                        if(agp.save()){//保存中奖信息
                            if(richService.lessOnePrize(ap.getId()) >= 1){//奖品数量减一
                                prize = ap.getId();
                            }
                        }
                        setAttr("valuep",ap.getPrizeValue());///奖品价值
                        setAttr("namep",ap.getPrizeName());///奖品名
                        setAttr("nop",agp.getPrizeNum());///奖品编号
                        bz=3;//中奖
                    }
                }
            }
        }
        setAttr("success",bz);
        renderJson();
    }


}
